package com.itboye.ihomebank.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

import com.itboye.ihomebank.adapter.FangYuanRenZhengAdpter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2018/6/27 0027.
 */

public class MyAsyTask extends AsyncTask<String, Void, String> {
    private  File file;
    int type;
    List<String> list;
    FangYuanRenZhengAdpter adapter;
    ScrapImg scrapImg;
    public MyAsyTask(File file2 , int type, List<String> list, FangYuanRenZhengAdpter adapter,ScrapImg scrapImg){
        this.type=type;
        this.list=list;
        this.file=file2;
        this.adapter=adapter;
        this.scrapImg=scrapImg;
    }

    @Override
    protected String doInBackground(String... strings) {


        FileOutputStream b02 = null;
                Log.d("==========shijian",System.currentTimeMillis()+"");
                Bitmap bitmap02 = BitmapFactory.decodeFile(file.getAbsolutePath());
                Log.d("==========shijian",System.currentTimeMillis()+"");

                Bitmap ratio = new ImageFactory().ratio(bitmap02, 1000, 750);
                Log.d("==========shijian",System.currentTimeMillis()+"");
                try {
                    b02 = new FileOutputStream(file.getAbsolutePath());
                    bitmap02.compress(Bitmap.CompressFormat.JPEG, 100, b02);// 把数据写入文件
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        b02.flush();
                        b02.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                Log.d("==========shijian",System.currentTimeMillis()+"");
                // Bitmap bit03 = BitmapFactory.decodeFile(fileName02);

                //先删除 临时文件 再覆盖显示临时文件
//                if (type == 1) {
//                    list.add(file.getAbsolutePath());
//                    adapter1.notifyDataSetChanged();
//                    // fangchanzheng.setImageBitmap(bt04); // 把图片显示在ImageView控件上
//                } else if (type == 2) {
//                    listImg2.add(fileName02);
//                    adapter2.notifyDataSetChanged();
//                    // weituoshu.setImageBitmap(bt04); // 把图片显示在ImageView控件上
//                } else if (type == 3) {
//                    listImg3.add(fileName02);
//                    adapter3.notifyDataSetChanged();
//                    // weituoshu.setImageBitmap(bt04); // 把图片显示在ImageView控件上
//                }
                 if(list.size()>0){
                     scrapImg.complete();
                     list.remove(list.size()-1);
                     list.add(file.getAbsolutePath());
                     adapter.notifyDataSetChanged();
                 }

        return null;
    }

    public interface ScrapImg{
        void complete();

    }

}
